package cn.ekuma.security.impl;

import com.openbravo.data.basic.BasicException;

import cn.ekuma.security.I_SecurityManager;
import cn.ekuma.security.I_SecurityObject;
import cn.ekuma.security.bean.I_GroupPermission;
import cn.ekuma.security.bean.I_Permission;
import cn.ekuma.security.bean.I_Role;
import cn.ekuma.security.bean.I_RolePermission;
import cn.ekuma.security.datalogic.I_DataLogicSecurity;

public abstract class AbstractSecurityManager implements I_SecurityManager {
	 protected I_Role role;
	 
	 protected I_RolePermission permissions;
	 protected I_DataLogicSecurity dlSecurity;
	 
	 String modeName;
	 
	public AbstractSecurityManager(I_DataLogicSecurity dlSystem,String modeName) {
		this.dlSecurity=dlSystem;
		this.modeName=modeName;
	}


	@Override
	public void fillSecurityObject(I_SecurityObject toFillObj) {
		int permissionId=getPermissionIdByName(toFillObj.getBaseSecurityName());
		if(hasBasePermission(permissionId))
		    toFillObj.setExtPermission(permissions.getExtPermission(permissionId));
		else if(hasPermission(permissionId))
				toFillObj.setExtPermission(I_Permission.ADMIN);
		else
			toFillObj.setExtPermission(I_Permission.NO_PASS);
	}



	public boolean hasGroupPermission(int permissionId){
		int i=permissionId;
		if((permissionId&I_GroupPermission.GROUP_FORE_FILTER)>0)
			i=permissionId&I_GroupPermission.GROUP_THREE;
		else if((permissionId&I_GroupPermission.GROUP_THREE_FILTER)>0)
			i=permissionId&I_GroupPermission.GROUP_TWO;
		else if((permissionId&I_GroupPermission.GROUP_TWO_FILTER)>0)
			i=permissionId&I_GroupPermission.GROUP_ONE;
		else
			return false;
		if(hasBasePermission(i))
			return true;
		return hasGroupPermission(i);
	}
	
	@Override
	public boolean hasPermission(int permissionId) {
		if(hasBasePermission(permissionId))
			return true;
		return hasGroupPermission(permissionId);
	}
	
	private boolean hasBasePermission(int permissionId) {
		if(permissions.hasPermission(permissionId))
			return true;
		return false;
	}
	
	
	@Override
	public void init(I_Role role) {
		this.role=role;	
		permissions=fillPermissionByRole(role);
		
	}
	
	public boolean hasPermissionByName(String name){
		return permissions.hasPermissionByName(name);
	}
	
	public int getPermissionIdByName(String name){
		return permissions.getPermissionIdByName(name);
	}

	public I_RolePermission fillPermissionByRole(I_Role role) {
		AbstractRolePermission baseRolePermission=new AbstractRolePermission();
		try {
			baseRolePermission.setPermissions( dlSecurity.loadRolePermission(role.getID(), modeName));
		} catch (BasicException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return baseRolePermission;
	}
	
	
	@Override
	public boolean hasePermissionByRole(I_Role role, String permission) {
		if(this.role.getID()==role.getID())
			return hasPermissionByName(permission);
		try {
			return dlSecurity.hasePermissionByRole(role.getID(),modeName,permission);
		} catch (BasicException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}


	@Override
	public boolean hasChildPermissionByName(String parentPermission,
			String childPermisson) {
		try {
			return dlSecurity.hasePermissionByRole(role.getID(),modeName,parentPermission,childPermisson);
		} catch (BasicException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
    public boolean lock(String askFor,String target, String targetId,String memo){
    	return dlSecurity.lock(askFor, target, targetId, memo);
    }
    
    public boolean unlock(String askFor,String target, String targetId){
    	return dlSecurity.unlock(askFor, target, targetId);
    }
    
    public boolean unlockAllbyAskToTarget(String askFor,String target){
    	return dlSecurity.unlockAllbyAskToTarget(askFor, target);
    }
    
    public boolean unlockAllbyAsk(String askFor){
    	return dlSecurity.unlockAllbyAsk(askFor);
    }
	
}
